﻿using Engine;
using Game;
using GameEntitySystem;
using System;
using TemplatesDatabase;
using Random = Game.Random;

namespace SAGhoul.Tartareosity
{
	public class SubsystemEntityBlockBehavior : SubsystemBlockBehavior
	{
		public override int[] HandledBlocks => [45, 64, 27, 216];

		public SubsystemBlockEntities SubsystemBlockEntities;

		public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
		{
			Point3 point3 = new(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z);
			ComponentBlockEntity blockEntity = SubsystemBlockEntities.GetBlockEntity(point3.X, point3.Y, point3.Z);
			//交互时，如果箱子、熔炉、工作台和发射器的方块实体为空，则创建方块实体并添加指定物品
			if (blockEntity == null && componentMiner.ComponentPlayer != null)
			{
				int id = Project.FindSubsystem<SubsystemTerrain>().Terrain.GetCellContents(point3.X, point3.Y, point3.Z);
				switch (id)
				{
					case 45:
					{
						DatabaseObject databaseObject = Project.GameDatabase.Database.FindDatabaseObject("Chest", Project.GameDatabase.EntityTemplateType, throwIfNotFound: true);
						ValuesDictionary valuesDictionary = [];
						valuesDictionary.PopulateFromDatabaseObject(databaseObject);
						valuesDictionary.GetValue<ValuesDictionary>("BlockEntity").SetValue("Coordinates", point3);
						Entity entity = Project.CreateEntity(valuesDictionary);
						Project.AddEntity(entity);
						ComponentChest componentChest = entity.FindComponent<ComponentChest>(throwOnError: true);
						if (point3 == (3469, 67, 3227))//隐士花园地下宝藏
						{
							componentChest.m_slots[0].Value = 965;
							componentChest.m_slots[0].Count = 1;
						}
						else if (point3 == (3340, 75, 3270) || point3 == (3342, 76, 3206) || point3 == (3342, 76, 3196) || point3 == (3139, 75, 3265) || point3 == (3111, 73, 3352) || point3 == (3147, 83, 3387) || point3 == (3180, 76, 3345) || point3 == (3470, 75, 3366) || point3 == (3344, 71, 3165))
						{
							componentChest.m_slots[0].Value = 459739;
							componentChest.m_slots[0].Count = 1;
						}
						else
						{
							for (int i = 0; i < 15; i++)
							{
								int itemnum = new Random().Int(1, BlocksManager.Blocks.Length - 1);
								if (BlocksManager.Blocks[itemnum].DefaultDisplayName == "空气" || itemnum == 965)//不能出现宇宙飞船
								{
									continue;
								}
								componentChest.m_slots[i].Value = itemnum;
								componentChest.m_slots[i].Count = new Random().Int(1, 3);
							}
						}
						componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new ChestWidget(componentMiner.Inventory, componentChest);
						AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
						return true;
					}
					case 64:
					{
						DatabaseObject databaseObject = Project.GameDatabase.Database.FindDatabaseObject("Furnace", Project.GameDatabase.EntityTemplateType, throwIfNotFound: true);
						ValuesDictionary valuesDictionary = [];
						valuesDictionary.PopulateFromDatabaseObject(databaseObject);
						valuesDictionary.GetValue<ValuesDictionary>("BlockEntity").SetValue("Coordinates", point3);
						Entity entity = Project.CreateEntity(valuesDictionary);
						Project.AddEntity(entity);
						ComponentFurnace componentFurnace = entity.FindComponent<ComponentFurnace>(throwOnError: true);
						int[] items = [77, 88, 176];
						if (new Random().Float(0, 1f) < 0.4f)
						{
							componentFurnace.m_slots[0].Value = items[new Random().Int(0, items.Length - 1)];
							componentFurnace.m_slots[0].Count = 1;
						}
						componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new FurnaceWidget(componentMiner.Inventory, componentFurnace);
						AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
						return true;
					}
					case 27:
					{
						DatabaseObject databaseObject = Project.GameDatabase.Database.FindDatabaseObject("CraftingTable", Project.GameDatabase.EntityTemplateType, throwIfNotFound: true);
						ValuesDictionary valuesDictionary = [];
						valuesDictionary.PopulateFromDatabaseObject(databaseObject);
						valuesDictionary.GetValue<ValuesDictionary>("BlockEntity").SetValue("Coordinates", point3);
						Entity entity = Project.CreateEntity(valuesDictionary);
						Project.AddEntity(entity);
						ComponentCraftingTable componentCraftingTable = entity.FindComponent<ComponentCraftingTable>(throwOnError: true);
						int[] items = [29, 165, 37, 222, 36, 38, 218, 219, 171, 169, 90, 117, 121, 120, 230];
						for (int i = 0; i < 9; i++)
						{
							if (new Random().Float(0, 1f) < 0.1f)
							{
								componentCraftingTable.m_slots[i].Value = items[new Random().Int(0, items.Length - 1)];
								componentCraftingTable.m_slots[i].Count = 1;
							}
						}
						componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new CraftingTableWidget(componentMiner.Inventory, componentCraftingTable);
						AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
						return true;
					}
					case 216:
					{
						DatabaseObject databaseObject = Project.GameDatabase.Database.FindDatabaseObject("Dispenser", Project.GameDatabase.EntityTemplateType, throwIfNotFound: true);
						ValuesDictionary valuesDictionary = [];
						valuesDictionary.PopulateFromDatabaseObject(databaseObject);
						valuesDictionary.GetValue<ValuesDictionary>("BlockEntity").SetValue("Coordinates", point3);
						Entity entity = Project.CreateEntity(valuesDictionary);
						Project.AddEntity(entity);
						ComponentDispenser componentDispenser = entity.FindComponent<ComponentDispenser>(throwOnError: true);
						componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new DispenserWidget(componentMiner.Inventory, componentDispenser);
						AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
						return true;
					}
				}
			}
			return false;
		}

		public override void Load(ValuesDictionary valuesDictionary)
		{
			SubsystemBlockEntities = Project.FindSubsystem<SubsystemBlockEntities>(true);
			SubsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
		}
	}
}
